import BaseEntity from '../../BaseEntity'

export class User<T> extends BaseEntity<T> {
    public url = this.options.baseURL + 'api/v1/users'

    constructor(data?: T) {
        super(data!)
    }

    async login() {
        return await this.getInstance()
            .post(`${this.url}/login`)
            .then(res => {
                return res.data
            })
    }

    async register() {
        return await this.getInstance()
            .post(`${this.url}/join`)
            .then(res => {
                return res.data
            })
    }

    async confirmMail() {
        return await this.getInstance()
            .put(`${this.url}/${(this.data as any).userId}/email/confirmed`)
            .then(res => {
                return res.data
            })
    }

    async requireConfirmMail(userId: string) {
        return await this.getInstance()
            .post(`${this.url}/${userId}/trust/email`)
            .then(res => {
                return res.data
            })
    }

    async getUserProfile(userId: string) {
        return await this.getInstance()
            .get(`${this.url}/${userId}/profile`)
            .then(res => {
                return res.data
            })
    }

    async editUserProfile(userId: string) {
        return await this.getInstance()
            .put(`${this.url}/${userId}/profile/edit`)
            .then(res => {
                return res.data
            })
    }

    async createUserAddress(userId: string) {
        return await this.getInstance()
            .post(`${this.url}/${userId}/addresses/create`)
            .then(res => {
                return res.data
            })
    }

    async getUserAddress(userId: string) {
        return await this.getInstance()
            .get(`${this.url}/${userId}/addresses`)
            .then(res => {
                return res.data
            })
    }

    async getUserAddressById(userId: string, addressId: string) {
        return await this.getInstance()
            .get(`${this.url}/${userId}/addresses/${addressId}`)
            .then(res => {
                return res.data
            })
    }

    async editUserAddress(userId: string, addressId: string) {
        return await this.getInstance()
            .put(`${this.url}/${userId}/addresses/${addressId}/edit`)
            .then(res => {
                return res.data
            })
    }

    async deleteUserAddress(userId: string, addressId: string) {
        return await this.getInstance()
            .delete(`${this.url}/${userId}/addresses/${addressId}/delete`)
            .then(res => {
                return res.data
            })
    }
}

export default 'User'
